﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using Etong.Web;
/// <summary>
///ShopCar 的摘要说明
/// </summary>
public class ShopCar<T> where T:IProduct,new()
{
	public ShopCar()
	{
        this.AllProducts = new List<ShopCarItem<T>>();

        this.OnTransToOrderList += delegate { };

        this.OnEachItemTransToOrderDetail += delegate { };
	}

    public string CookieName { get; set; }
    public List<ShopCarItem<T>> AllProducts { get; set; }

    public float Discount { get; set; }

    #region 属性
    public float AllPrice
    {
        get 
        {
            return AllProducts.Sum(x=>(float)typeof(T).GetProperty(x.Item.GetPriceFieldName()).GetValue(x.Item,null));            
        }
    }

    public int AllCount
    {
        get
        {
            return AllProducts.Sum(x => x.BuyCount);
        }
    }

    public float AllPriceAfaterDiscount
    {
        get { return this.AllPrice * this.Discount; }
    }
    #endregion

    #region 操作
    public void Add(T item,int buyCount)
    {
        ShopCarItem<T> spItem = GetExistItem(item);
        if (spItem == null)
        {
            spItem = new ShopCarItem<T>();
            spItem.BuyCount = buyCount;
            spItem.Item = item;
            AllProducts.Add(spItem);
        }
        else
        {
            spItem.BuyCount += buyCount;
        }
        this.SaveToCookie();
    }
    public void Remove(T item)
    {
        ShopCarItem<T> spItem = GetExistItem(item);
        if (spItem != null)
        {
            this.AllProducts.Remove(spItem);
            this.SaveToCookie();
        }
    }
    public void Remove(string PK)
    {
        this.AllProducts.RemoveAll(x=>typeof(T).GetProperty(x.Item.GetPKFieldName()).GetValue(x.Item, null).ToString() == PK.ToString());

        this.SaveToCookie();
    }
    public void SetCount(T item, int count)
    {
        ShopCarItem<T> spItem = GetExistItem(item);
        if (spItem != null)
        {
            spItem.BuyCount = count;
            this.SaveToCookie();
        }
    }
    public void SetCount(string PK, int count)
    {
        foreach (var x in this.AllProducts.Where(x => typeof(T).GetProperty(x.Item.GetPKFieldName()).GetValue(x.Item, null).ToString() == PK))
        {
            x.BuyCount = count;
        }
        this.SaveToCookie();
    }

    public void Clear(string CookieName)
    {
        this.AllProducts.Clear();
        this.SaveToCookie(CookieName);
    }

    public void Clear()
    {
        this.Clear(this.CookieName);
    }

    private ShopCarItem<T> GetExistItem(T item)
    {
        ShopCarItem<T> spItem = null;
        PropertyInfo pinfo=typeof(T).GetProperty(item.GetPKFieldName());
        //HttpContext.Current.Response.Write(this.AllProducts.Exists(x => pinfo.GetValue(x.Item, null).ToString() == pinfo.GetValue(x.Item, null).ToString()));
        if (this.AllProducts.Any(x => pinfo.GetValue(x.Item, null).ToString() == pinfo.GetValue(item, null).ToString()))
        {
            spItem = this.AllProducts.First(x => pinfo.GetValue(x.Item, null).ToString() == pinfo.GetValue(item, null).ToString());
        }
        return spItem;
    }

    
    #endregion

    #region 存储

    public static ShopCar<T> LoadFromCookie(string CookieName)
    {
        HttpRequest Request = HttpContext.Current.Request;
        if (Request.Cookies[CookieName] == null || string.IsNullOrEmpty(Request.Cookies[CookieName].Value))
        {
            ShopCar<T> sp = new ShopCar<T>();
            sp.CookieName = CookieName;
            return sp;
        }
        else
        {
            string json = Request.Cookies[CookieName].Value;
            json = HttpContext.Current.Server.UrlDecode(json);
            return json.ToOBJ<ShopCar<T>>();
        }
    }

    public void SaveToCookie(string CookieName)
    {
        this.CookieName = CookieName;
        string json = this.ToJSON();
        json = HttpContext.Current.Server.UrlEncode(json);
        HttpResponse Response = HttpContext.Current.Response;
        HttpRequest Request = HttpContext.Current.Request;
        if (Request.Cookies[CookieName] == null || string.IsNullOrEmpty(Request.Cookies[CookieName].Value))
        {
            Response.Cookies.Add(new HttpCookie(CookieName, json));
        }
        else
        {
            Response.Cookies[CookieName].Value = json;
        }
    }

    public void SaveToCookie()
    {
        this.SaveToCookie(this.CookieName);
    }

    #endregion

    #region 转换

    public delegate void ShopCarTransToOrderList<T>(ShopCar<T> ShopCar,OrderList OrderList) where T:IProduct,new();
    public event ShopCarTransToOrderList<T> OnTransToOrderList;

    public void TransToOrderList(ShopCar<T> shopcar, OrderList OrderList)
    {
        OnTransToOrderList(shopcar, OrderList);
    }

    public delegate void ShopCarItemTransToOrderDetail<T>(T Product, OrderDetail OrderDetail) where T:IProduct,new();
    public event ShopCarItemTransToOrderDetail<T> OnEachItemTransToOrderDetail;
    
    public void TransToOrderDetail(T product, OrderDetail orderdetail)
    {
        OnEachItemTransToOrderDetail(product, orderdetail);
    }

    
    #endregion
}
